package com.youme.section.shop

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import com.hyphenate.easeim.R
import com.youme.section.shop.adapter.ViewPagerAdapter
import kotlinx.android.synthetic.main.layout_shop_details_content.view.*

class ShopContentLayout : ConstraintLayout {

    private lateinit var menuFragment: MenuFragment

    private lateinit var fargmentIsShow: FragmentShowListenerr

    private lateinit var showCarListenerr: ShowCarListenerr

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    private lateinit var mFragmentList: MutableList<Fragment>
    private var mShopContentBehavior: ShopContentBehavior? = null

    init {
        LayoutInflater.from(context).inflate(R.layout.layout_shop_details_content, this)
    }

    public fun fragmentIsShow(listenerr: FragmentShowListenerr) {
        menuFragment.fragmentShow(listenerr)
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        //点菜、评价、商家 Fragment
        mFragmentList = mutableListOf()
        menuFragment = MenuFragment.getInstance(object : MenuFragment.LayoutExpandControl {
            override fun fold() {
                mShopContentBehavior?.fold()
            }

            override fun isExpanded(): Boolean {
                return mShopContentBehavior?.isExpanded() ?: false
            }
        })
        mFragmentList.add(menuFragment)
        mFragmentList.add(EvaluateFragment.getInstance(object : EvaluateFragment.LayoutExpandControl {
            override fun fold() {
                mShopContentBehavior?.fold()
            }

            override fun isExpanded(): Boolean {
                return mShopContentBehavior?.isExpanded() ?: false
            }
        }))
        mFragmentList.add(ShopFragment.getInstance(object : ShopFragment.LayoutExpandControl {
            override fun fold() {
                mShopContentBehavior?.fold()
            }

            override fun isExpanded(): Boolean {
                return mShopContentBehavior?.isExpanded() ?: false
            }
        }))
        val vpAdapter = ViewPagerAdapter((context as AppCompatActivity).supportFragmentManager, mFragmentList)
        vp_main.adapter = vpAdapter
        vp_main.offscreenPageLimit = mFragmentList.size

        tab_layout.setViewPager(vp_main, arrayOf("菜品", "包间", "宴会厅"))
        showCarListenerr = menuFragment.getCarListener()
    }

    public fun getCarListener(): ShowCarListenerr {
        return showCarListenerr
    }

    public fun setAddListener(listenerr: AddCaiListenerr) {
        menuFragment.setAddListener2(listenerr)
    }

    fun getScrollableView(): View {
        val view =
            (mFragmentList[vp_main.currentItem] as ScrollableViewProvider).getScrollableView()
        return view
    }

    fun getRootScrollView(): View? {
        return (mFragmentList[vp_main.currentItem] as ScrollableViewProvider).getRootScrollView()
    }

    fun setShopContentBehavior(shopContentBehavior: ShopContentBehavior) {
        mShopContentBehavior = shopContentBehavior
    }
}

interface ScrollableViewProvider {
    fun getScrollableView(): View
    fun getRootScrollView(): View? {
        return null
    }
}